package dao;

import entity.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import utils.DruidUtil;

import java.sql.SQLException;

/**
 * @Author:SA
 * @DATA:DATA
 */


public class UserDaoImpl implements UserDao{
    @Override
    public int addUser(User user) {
        //注册
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into user(name,password)values(?,?)";
        try {
            int line = queryRunner.update(sql, user.getName(), user.getPassword());
            return line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return 0;
    }

    @Override
    public User select(User user) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select * from user where name = ? and where password = ?";
        try {
            User usr = queryRunner.query(sql, new BeanHandler<>(User.class), user.getName(), user.getPassword());
            return  usr;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }
}
